home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / WWW / MailRobot / Implementation / str.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-27  |  1.1 KB  |  59 lines

  1. /*        Case-independent string comparison
  2. **
  3. **    Version TBL Oct 91 replaces one which modified the strings.
  4. */
  5. #include <ctype.h>
  6.  
  7. #ifdef __STDC__
  8. #define CONST const
  9. #else
  10. #define CONST
  11. #endif
  12.     
  13. /*    Strings of any length
  14. **    ---------------------
  15. */
  16. #ifdef __STDC__
  17. int strcasecmp(const char*a, const char *b)
  18. #else
  19. int strcasecmp(a,b)
  20.     char *a, *b;
  21. #endif
  22. /*    Strictly tolower() is undefined unless toupper() is true. (!)
  23. */
  24. #define TOLOWER(c) (isupper(c) ? tolower(c) : c)
  25. {
  26.     CONST char *p =a;
  27.     CONST char *q =b;
  28.     for(p=a, q=b; *p && *q; p++, q++) {
  29.         int diff = TOLOWER(*p) - TOLOWER(*q);
  30.         if (diff) return diff;
  31.     }
  32.     if (*p) return 1;    /* p was longer than q */
  33.     return 0;        /* Exact match */
  34. }
  35.  
  36.  
  37. /*    With count limit
  38. **    ----------------
  39. */
  40. #ifdef __STDC__
  41. int strncasecmp(const char*a, const char *b, int n)
  42. #else
  43. int strncasecmp(a,b,n)
  44.     char *a, *b;
  45.     int n;
  46. #endif
  47. {
  48.     CONST char *p =a;
  49.     CONST char *q =b;
  50.     
  51.     for(p=a, q=b;; p++, q++) {
  52.         int diff;
  53.         if (p == a+n) return 0;    /*   Match up to n characters */
  54.         if (!(*p && *q)) return *p - *q;
  55.         diff = TOLOWER(*p) - TOLOWER(*q);
  56.         if (diff) return diff;
  57.     }
  58.     /*NOTREACHED*/
  59. }